/**
 * Log.java
 * Copyright (C) 2009 Jose Mauro da Silva Sandy
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

package utils;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

/**
 *
 * @author jmsandy
 */
public class Log {

    /**
     * Grava o Log do erro ocorrido
     * @param msg - mensagem a ser gravado
     * @param obj - objeto que ocorreu a excessao
     */
    public static void gravarLog(String msg, Object obj){
        try{
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
            Logger log = Logger.getLogger(obj.getClass().getName());

            FileHandler fh;
            String path = ApplicationPath.getApplicationPath() + "log" +
                          File.separator;

            File f = new File(path);
            if(!f.exists()){
                f.mkdir();
            }

            f = new File(path + sdf.format(new Date()) + ".log");

            fh = new FileHandler(f.getAbsolutePath(), 1048576, 1000, true);
            fh.setFormatter(new SimpleFormatter());
            log.addHandler(fh);
            log.warning(msg);
            log.removeHandler(fh);
            fh.close();
        }
        catch(Exception ex){
            ex.printStackTrace();
        }
    }

    /**
     * @return caminho do arquivo de log gravado
     */
    public static String getAbsolutePathLog(){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        return ApplicationPath.getApplicationPath() + "log" +
                          File.separator + sdf.format(new Date()) + ".log.X.X";
    }
}
